﻿package com._public._display{
	import flash.display.Sprite;

	/**
	 * ...2010-3-31 11:25
	 * @author WZH(shch8.com)
	 * 绘制扇形
	 * var sector:Sector = new Sector(CENTER*2, 30,0x888888, 0xeeeeee);
		this.addChild(sector); 
	*  // Sector(扇形半径,扇形比例,扇形颜色,圆形背景色,圆形背景形)
	 */
	public class Sector extends Sprite {

		private var _x:Number=0;//圆心坐标X   
		private var _y:Number=0;//圆心坐标Y   
		private var _r:Number;//半径   
		private var _mColor:uint;//扇型填充颜色   
		private var _dColor:int;//背景圆填充颜色   
		private var _lColor:int;//线颜色   
		private var _angle:Number;//扇型角度   
		private var _startAngle:Number;//开始角度   
		/**
		 * 
		 * @param	r			扇形半径
		 * @param	percent		扇形比例
		 * @param	sectorColor	扇形颜色
		 * @param	bgColor		圆形背景颜色,如果不设置或设为-1,表示不采用背景
		 * @param	bgLineColor	圆形背景线条颜色,如果不设置或设为-1,表示不采用线条
		 */
		public function Sector(sectorColor:uint, bgColor:int = -1, bgLineColor:int = -1) {
			this._lColor = bgLineColor;
			this._mColor = sectorColor;
			this._dColor = bgColor;
		}
		/**
		 * 
		 * @param	r			扇形半径
		 * @param	percent		扇形比例
		 */
		public function reDraw(r:Number, percent:uint):void {
			_x = _y =this._r = r;
			this._angle = percent * 360 / 100;
			this._startAngle = 270;
			graphics.clear()
			if(_dColor!=-1){
			if(_lColor!=-1)graphics.lineStyle(0, this._lColor);
			//画整个圆作为背景   
			graphics.beginFill(this._dColor, 1);
			graphics.drawCircle(this._x, this._y, this._r);
			graphics.endFill();
			}
			//画扇型   
			graphics.beginFill(this._mColor, 1);
			graphics.moveTo(this._x, this._y);
			this._angle = (Math.abs(this._angle) > 360)?360:this._angle;
			var n:Number = Math.ceil(Math.abs(this._angle) / 45);
			var angleA:Number = this._angle / n;
			angleA = angleA * Math.PI / 180;
			this._startAngle = this._startAngle * Math.PI / 180;
			graphics.lineTo(this._x + this._r * Math.cos(this._startAngle), this._y + this._r * Math.sin(this._startAngle));
			var bx:Number
			var by:Number
			for (var i:uint=1; i<=n; i++) {
				this._startAngle += angleA;
				var angleMid:Number = this._startAngle - angleA / 2;
				bx=this._x + this._r /Math.cos(angleA / 2) * Math.cos(angleMid);
				by=this._y + this._r /Math.cos(angleA / 2) * Math.sin(angleMid);
				graphics.curveTo(bx, by, this._x + this._r * Math.cos(this._startAngle),this._y + this._r * Math.sin(this._startAngle));
			}
			if (this._angle!=360) {
				graphics.lineTo(this._x, this._y);
			}
			graphics.endFill();
			bx=by=n=angleA=NaN
		}
	}
}
